package com.acxiom.bsh.controller;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.acxiom.bsh.constants.CommonConstant;
import com.acxiom.bsh.entity.QuerySigninResult;
import com.acxiom.bsh.entity.SigninResult;
import com.acxiom.bsh.service.SigninService;

@Controller
public class SigninController {

	@Autowired
	private SigninService siginService;

	/**
	 * 签到
	 * 
	 * @Title: signin
	 * @param session
	 *            存储淘宝用户信息会话
	 * @return
	 */
	@RequestMapping(value = "/signin")
	@ResponseBody
	public SigninResult signin(HttpSession session) {
		String mixNick = getNickName(session);
		if (mixNick == null) {
			return null;
		}
		SigninResult result = null;
		try {
			result = siginService.signin(mixNick);
		} catch (Exception e) {
			if (result == null) {
				result = new SigninResult();
			}
			if (StringUtils.isEmpty(result.getCode())) {
				result.setCode("099");
				result.setMessage("系统正忙，请稍后");
			}
		}
		return result;
	}

	/**
	 * 签到记录
	 * 
	 * @Title: signinList
	 * @param session
	 *            存储淘宝用户信息会话
	 * @return
	 */
	@RequestMapping(value = "/signin/list")
	@ResponseBody
	public QuerySigninResult signinList(HttpSession session) {
		String mixNick = getNickName(session);
		if (mixNick == null) {
			return null;
		}
		QuerySigninResult result = null;
		try {
			result = siginService.querySignin(mixNick);
		} catch (Exception e) {
			if (result == null) {
				result = new QuerySigninResult();
			}
			if (StringUtils.isEmpty(result.getCode())) {
				result.setCode("099");
				result.setMessage("系统正忙，请稍后");
			}
		}
		return result;
	}

	/**
	 * 获取用户nick信息
	 * 
	 * @Title: getNickName
	 * @param session
	 *            存储淘宝用户信息会话
	 * @return
	 */
	private String getNickName(HttpSession session) {
		Object obj = session.getAttribute(CommonConstant.TAOBAO_NICK_ATTRIBUTE);
		String nickName = null;
		if (obj != null) {
			nickName = obj.toString();
		}
		return nickName;
	}
}
